package com.heima.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.apis.wemedia.IWemediaClient;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.dtos.AuthDto;
import com.heima.model.user.pojos.ApUser;
import com.heima.model.user.pojos.ApUserRealname;
import com.heima.user.mapper.ApUserMapper;
import com.heima.user.mapper.ApUserRealNameMapper;
import com.heima.user.service.ApUserRealnameService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ApUserRealnameServiceImpl extends ServiceImpl<ApUserRealNameMapper,ApUserRealname> implements ApUserRealnameService {
    @Autowired
    private ApUserRealNameMapper apUserRealNameMapper;
    @Autowired
    private IWemediaClient iWemediaClient;
    @Autowired
    private ApUserMapper apUserMapper;
    /**
     * 分页查询认证列表
     * @param dto
     * @return
     */
    @Override
    public ResponseResult findList(AuthDto dto) {
        //检查参数
        dto.checkParam();
        //分页查询
        IPage page = new Page(dto.getPage(),dto.getSize());
        LambdaQueryWrapper<ApUserRealname> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        //根据状态查询
        if (dto.getStatus() != null){
            lambdaQueryWrapper.eq(ApUserRealname::getStatus,dto.getStatus());
        }
        //结果返回
        page = page(page,lambdaQueryWrapper);
        PageResponseResult responseResult = new PageResponseResult(dto.getPage(), dto.getSize(), (int) page.getTotal());
        responseResult.setData(page.getRecords());
        return responseResult;
    }

    /**
     * 用户审核失败
     * @param dto
     * @return
     */
    @Override
    public ResponseResult authFail(AuthDto dto) {
        //判断dto是否为空
        if (dto == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        ApUserRealname apUserRealname = apUserRealNameMapper.selectById(dto.getId());
        //填写驳回原因
        apUserRealname.setReason(dto.getMsg());
        //改变状态
        apUserRealname.setStatus(AuthDto.Status.FAIL.getCode());
        apUserRealNameMapper.updateById(apUserRealname);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 通过审核
     * @param dto
     * @return
     */
    @Override
    public ResponseResult authPass(AuthDto dto) {
        boolean flag = Boolean.FALSE;
        //判断dto是否为空
        if (dto == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        ApUserRealname apUserRealname = apUserRealNameMapper.selectById(dto.getId());
        //通过审核改变状态
        if (apUserRealname == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"用户不存在");
        }
        //判断审核状态是否为待审核
        if (apUserRealname.getStatus()==1){
            apUserRealname.setStatus(AuthDto.Status.ADOPT.getCode());
            apUserRealNameMapper.updateById(apUserRealname);
            flag = Boolean.TRUE;
        }
        if (flag){
            //根据userid查询 用户表apuser
            ApUser apUser = apUserMapper.selectById(dto.getId());
            if (apUser == null){
                return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"用户不存在");
            }
            //调用远程微服务开通自媒体账号
            iWemediaClient.saveWmUser(apUser);
        }
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
